package com.demostic.demostic_test.ui;

import android.content.Context;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
import android.widget.HorizontalScrollView;
import android.widget.LinearLayout;

import androidx.recyclerview.widget.RecyclerView;

import com.demostic.demostic_test.adapter.DevListAdapter_new;
import com.demostic.demostic_test.utils.ScreenUtil;

public class SlidingMenu extends HorizontalScrollView {
    private static final float radio = 0.3f;
    private float downX;
    private boolean isOpen;
    private CustomOnClickListener mCustomOnClickListener;
    private int mMenuWidth;
    private final int mScreenWidth;
    private boolean once = true;
    private float upX;

    public interface CustomOnClickListener {
        void onClick();
    }

    public SlidingMenu(Context context, AttributeSet attrs) {
        super(context, attrs);
        this.mScreenWidth = ScreenUtil.getScreenWidth(context);
        setOverScrollMode(2);
        setHorizontalScrollBarEnabled(false);
    }

    public void closeMenu() {
        smoothScrollTo(0, 0);
        this.isOpen = false;
    }

    public boolean isOpen() {
        return this.isOpen;
    }

    private DevListAdapter_new getAdapter() {
        View view = this;
        do {
            view = (View) view.getParent();
        } while (!(view instanceof RecyclerView));
        return (DevListAdapter_new) ((RecyclerView) view).getAdapter();
    }

    private void onOpenMenu() {
        getAdapter().holdOpenMenu(this);
        this.isOpen = true;
    }

    private void closeOpenMenu() {
        if (!this.isOpen) {
            getAdapter().closeOpenMenu();
        }
    }

    public SlidingMenu getScrollingMenu() {
        return getAdapter().getScrollingMenu();
    }

    public void setScrollingMenu(SlidingMenu scrollingMenu) {
        getAdapter().setScrollingMenu(scrollingMenu);
    }

    /* access modifiers changed from: protected */
    @Override // android.widget.HorizontalScrollView, android.widget.FrameLayout, android.view.View
    public void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        if (this.once) {
            LinearLayout wrapper = (LinearLayout) getChildAt(0);
            wrapper.getChildAt(0).getLayoutParams().width = this.mScreenWidth;
            this.mMenuWidth = wrapper.getChildAt(1).getLayoutParams().width;
            this.once = false;
        }
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
    }

    @Override // android.widget.HorizontalScrollView, android.view.View
    public boolean onTouchEvent(MotionEvent ev) {
        if (getScrollingMenu() != null && getScrollingMenu() != this) {
            return false;
        }
        switch (ev.getAction()) {
            case 1:
                this.upX = ev.getX();
                setScrollingMenu(null);
                int scrollX = getScrollX();
                if (this.upX - this.downX > 10.0f) {
                    smoothScrollTo(0, 0);
                    return false;
                } else if (Math.abs(scrollX) <= 10) {
                    if (this.mCustomOnClickListener == null) {
                        return false;
                    }
                    this.mCustomOnClickListener.onClick();
                    return false;
                } else if (Math.abs(scrollX) > this.mMenuWidth / 2) {
                    smoothScrollTo(this.mMenuWidth, 0);
                    onOpenMenu();
                    return false;
                } else {
                    smoothScrollTo(0, 0);
                    return false;
                }
            case 0:
                this.downX = ev.getX();
                closeOpenMenu();
                setScrollingMenu(this);
                break;
        }
        return super.onTouchEvent(ev);
    }

    public void setCustomOnClickListener(CustomOnClickListener listener) {
        this.mCustomOnClickListener = listener;
    }
}
